/*
 * CodeSaveCommand.java
 *
 * Created on 2006년 3월 29일 (수), 오후 4:11
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.qbooks.app.store;

import java.util.*;





import javax.servlet.*;
import javax.servlet.http.*;

import org.json.simple.JSONObject;

import com.zll.framework.model.HashBox;
import com.zll.framework.model.HashList;
import com.zll.framework.servlet.Command;
import com.zll.framework.util.StringUtil;
import com.qbooks.app.AppDAO;
import com.qbooks.common.CommonUtil;





/**
 *
 * @author eastar
 */
public class StoreHomeCommand implements Command
{
	
    public String execute(HttpServletRequest  request, HttpServletResponse response)
    throws Exception
    {
    	AppDAO dao = new AppDAO();
    	

    	HashBox box = new HashBox(request);
    	
    	String os	= StringUtil.nvl(request.getAttribute("os"));
    	String id	= StringUtil.nvl(request.getParameter("id"));
    	
    	HashMap infos = new HashMap();
    	infos.put("member_id", id);
    	
    	System.out.println("id:"+ id);
    	
    	/* 자동 로그인 처리 */
    	HttpSession session = request.getSession();
    	
    	String sessionUserId = StringUtil.nvl(session.getAttribute("session.user_id"));
    	
    	//System.out.println("session:" + sessionUserId);
    	
    	//로그인이 안되어 있는 경우, 자동로그인
    	
    	if ("".equals(sessionUserId)) 
    	{
	    	if ("".equals(id)) {
	    		session.invalidate();	//id가 파라미터로 오면 회원정보 조회, 없으면 로그아웃
	    		System.out.println("session invlaid");
	    	} else {
	    		HashList list = dao.selectMember(infos);
	//System.out.println(list.toString());
				if (list.size() == 1) {
		    		session.setAttribute("session.user_id", list.get("member_id",0));
					session.setAttribute("session.user_name", list.get("member_name",0));
					session.setAttribute("session.user_auth", "GE");
				} else {
					session.invalidate();
				}
	    	}
    	}
    	else if (!sessionUserId.equals(id)) {		//다른 사용자로 로그인
    		if ("".equals(id)) {
    			session.invalidate();	
	    	} else {
    			HashList list = dao.selectMember(infos);
    			//System.out.println(list.toString());
				if (list.size() == 1) {
		    		session.setAttribute("session.user_id", list.get("member_id",0));
					session.setAttribute("session.user_name", list.get("member_name",0));
					session.setAttribute("session.user_auth", "GE");
				} else {
					session.invalidate();
				}
    		}
    	}
    	


		if ("android".equals(os)) {
			
		}
		else if ("ios".equals(os)) {
			
		}

		return "/m/store/categoryList.jsp";
    }
}
